package com.xiaomeng.principle.ocp.improve;

public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
        graphicEditor.drawShape(new OtherGraphic());
    }
}

// 绘图类
class GraphicEditor {
    public void drawShape(Shape s) {
        s.draw();
    }
}

// Shape类 基类
abstract class Shape {
    int m_ype;
    public abstract void draw();
}
class Rectangle extends Shape {
    Rectangle() {
        super.m_ype = 1;
    }
    @Override
    public void draw() {
        System.out.println("绘制矩形");
    }
}

class Circle extends Shape {
    Circle() {
        super.m_ype = 2;
    }
    @Override
    public void draw() {
        System.out.println("绘制圆形");
    }
}

// 新增画三角形
class Triangle extends Shape {
    Triangle() {
        super.m_ype = 3;
    }
    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}
// 新增一个图形
class OtherGraphic extends Shape {

    OtherGraphic () {
        super.m_ype = 4;
    }
    @Override
    public void draw() {
        System.out.println("绘制其他图形");
    }
}